rm(list=ls())

library(foreign)
library(ggplot2)


############################## Parkland RDD ############################## 
setwd("/Users//johnholbein/Dropbox (Batten School @ UVA)/Work/School Shootings/Data/Google Trends")

all3<-read.csv("parklandappendedadjusted.csv")

color.names <- c("#000000", "#737373", "#bdbdbd")

attach(all3)

plot1<-ggplot(all3, aes(y=gun_control,  x=rv))+ 
  stat_smooth(data=all3, colour="#7fbf7b", aes(y=gun_control,  x=rv), size=2) +
  stat_smooth(data=all3, colour="#7fbf7b", aes(y=gun_control_2,  x=rv_2),  size=2) + #Gun Control
  
  
  stat_smooth(data=all3, colour="#762a83", aes(y=register_to_vote,  x=rv), size=2) +
  stat_smooth(data=all3, colour="#762a83", aes(y=register_to_vote_2,  x=rv_2),  size=2) + # register to vote
  
  
  geom_vline(aes(xintercept=0), colour="grey8", linetype="dashed", size=2)  + theme_bw() + theme(panel.grid.major = element_blank(), 
                                                                                                 panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(legend.title = element_blank()) +
  labs(x="Day Relative to Shooting", y="Google Search Popularity", title="") +
  theme(text = element_text(size=30)) +
  annotate("text", x=15, y = 90, label = "Gun Control", size=12, fontface="bold", colour="#7fbf7b", angle=0) +
  annotate("text", x=-20, y = 22, label = "Register to Vote", size=12, fontface="bold", colour="#762a83", angle=0) +
  annotate("text", x=-2, y = 95, label = "Shooting", size=12, fontface="bold", colour="grey8", angle=90) +  ylim(-10, 120)


plot1 
ggsave(plot1, file="parkland_gun_control.png", width=7, height=7, scale=2)

##### FLORIDA ONLY
setwd("/Users//johnholbein/Dropbox (Batten School @ UVA)/Work/School Shootings/Data/Google Trends")

all3<-read.csv("parklandappendedadjustedFLORIDAONLY.csv")

color.names <- c("#000000", "#737373", "#bdbdbd")

attach(all3)

plot1<-ggplot(all3, aes(y=gun_control,  x=rv))+ 
  stat_smooth(data=all3, colour="#7fbf7b", aes(y=gun_control,  x=rv), size=2) +
  stat_smooth(data=all3, colour="#7fbf7b", aes(y=gun_control_2,  x=rv_2),  size=2) + #Gun Control
  
  
  stat_smooth(data=all3, colour="#762a83", aes(y=register_to_vote,  x=rv), size=2) +
  stat_smooth(data=all3, colour="#762a83", aes(y=register_to_vote_2,  x=rv_2),  size=2) + # register to vote
  
  
  geom_vline(aes(xintercept=0), colour="grey8", linetype="dashed", size=2)  + theme_bw() + theme(panel.grid.major = element_blank(), 
                                                                                                 panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(legend.title = element_blank()) +
  labs(x="Day Relative to Shooting", y="Google Search Popularity", title="") +
  theme(text = element_text(size=30)) +
  annotate("text", x=10, y = 90, label = "Gun Control", size=12, fontface="bold", colour="#7fbf7b", angle=0) +
  annotate("text", x=-20, y = 12, label = "Register to Vote", size=12, fontface="bold", colour="#762a83", angle=0) +
  annotate("text", x=-2, y = 95, label = "Shooting", size=12, fontface="bold", colour="grey8", angle=90) +  ylim(-10, 120)


plot1 
ggsave(plot1, file="parkland_gun_control_florida_only.png", width=7, height=7, scale=2)

############################## Sandy Hook RDD ############################## 
setwd("/Users//johnholbein/Dropbox (Batten School @ UVA)/Work/School Shootings/Data/Google Trends")

all3<-read.csv("sandyhookmonth.csv")

color.names <- c("#000000", "#737373", "#bdbdbd")

attach(all3)

plot1<-ggplot(all3, aes(y=gun_control,  x=rv))+ 
  stat_smooth(data=all3, colour="#7fbf7b", aes(y=gun_control,  x=rv), size=2, se=TRUE) +
  stat_smooth(data=all3, colour="#7fbf7b", aes(y=gun_control_2,  x=rv_2),  size=2, se=TRUE) + #Gun Control
  
  
  stat_smooth(data=all3, colour="#762a83", aes(y=register_to_vote,  x=rv), size=2) +
  stat_smooth(data=all3, colour="#762a83", aes(y=register_to_vote_2,  x=rv_2),  size=2)+ # register to vote
  
  
  geom_vline(aes(xintercept=0), colour="grey8", linetype="dashed", size=2)  + theme_bw() + theme(panel.grid.major = element_blank(), 
                                                                                                 panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(legend.title = element_blank()) +
  labs(x="Day Relative to Shooting", y="Google Search Popularity", title="") +
  theme(text = element_text(size=30)) +
  annotate("text", x=12, y = 90, label = "Gun Control", size=12, fontface="bold", colour="#7fbf7b", angle=0) +
  annotate("text", x=-20, y = -5, label = "Register to Vote", size=12, fontface="bold", colour="#762a83", angle=0) +
  annotate("text", x=-2, y = 95, label = "Shooting", size=12, fontface="bold", colour="grey8", angle=90) + ylim(-10, 120)  


plot1 
ggsave(plot1, file="sandy_hook_gun_control.png", width=7, height=7, scale=2)

##### CONNECTICUT ONLY
setwd("/Users//johnholbein/Dropbox (Batten School @ UVA)/Work/School Shootings/Data/Google Trends")

all3<-read.csv("sandyhookmonthCONNECTICUTONLY.csv")

color.names <- c("#000000", "#737373", "#bdbdbd")

attach(all3)

plot1<-ggplot(all3, aes(y=gun_control,  x=rv))+ 
  stat_smooth(data=all3, colour="#7fbf7b", aes(y=gun_control,  x=rv), size=2, se=TRUE) +
  stat_smooth(data=all3, colour="#7fbf7b", aes(y=gun_control_2,  x=rv_2),  size=2, se=TRUE) + #Gun Control
  
  
  stat_smooth(data=all3, colour="#762a83", aes(y=register_to_vote,  x=rv), size=2) +
  stat_smooth(data=all3, colour="#762a83", aes(y=register_to_vote_2,  x=rv_2),  size=2)+ # register to vote
  
  
  geom_vline(aes(xintercept=0), colour="grey8", linetype="dashed", size=2)  + theme_bw() + theme(panel.grid.major = element_blank(), 
                                                                                                 panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(legend.title = element_blank()) +
  labs(x="Day Relative to Shooting", y="Google Search Popularity", title="") +
  theme(text = element_text(size=30)) +
  annotate("text", x=9, y = 90, label = "Gun Control", size=12, fontface="bold", colour="#7fbf7b", angle=0) +
  annotate("text", x=-20, y = -5, label = "Register to Vote", size=12, fontface="bold", colour="#762a83", angle=0) +
  annotate("text", x=-2, y = 95, label = "Shooting", size=12, fontface="bold", colour="grey8", angle=90) + ylim(-10, 120)  


plot1 
ggsave(plot1, file="sandy_hook_gun_control_connecticut_only.png", width=7, height=7, scale=2)



############################## Main Effect ############################## 

setwd("/Users/johnholbein/Dropbox (Batten School @ UVA)/Work/School Shootings/Data/Google Trends")

diffdiffall<-read.dta("/Users/johnholbein/Dropbox (Batten School @ UVA)/Work/School Shootings/Data/Google Trends/main_effect.dta")

attach(diffdiffall) 



plot1<-ggplot(diffdiffall, aes(y=coef,  x=reorder(estimate, num))) + 
  geom_hline(aes(yintercept=0), colour="#990000", linetype="dashed", size=2) +
  geom_point(data=diffdiffall, size=19,  colour = "black") +
  geom_errorbar(data=diffdiffall, aes(ymin=coef-1.96*stderr, ymax= coef+1.96*stderr),  colour = "black", width=0, size=3) +
  geom_errorbar(data=diffdiffall, aes(ymin=coef-1.64*stderr, ymax= coef+1.64*stderr),  colour = "black", width=0, size=5) +
  theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(legend.position="none") +
  theme(legend.title = element_blank()) +
  labs(x="Effect Estimate", y="Effect on Google Searches for Gun Control (Std.)", title="") +
  theme(text = element_text(size=30)) + 
  scale_y_continuous(limits=c(-.1, .5)) 

plot1

ggsave(plot1, file="main_effect.png", width=6, height=6, scale=2)



############################## Taper Off ############################## 

diffdiffall<-read.dta("/Users/johnholbein/Dropbox (Batten School @ UVA)/Work/School Shootings/Data/Google Trends/taper_off.dta")
attach(diffdiffall) 

plot1<-ggplot(diffdiffall, aes(y=coef,  x=days_since_shooting)) + 
  geom_hline(aes(yintercept=0), colour="#990000", linetype="dashed", size=2) +
  geom_point(data=diffdiffall, size=5,  colour = "black") +
  geom_errorbar(data=diffdiffall, aes(ymin=coef-1.96*stderr, ymax= coef+1.96*stderr),  colour = "black", width=0, size=1) +
  geom_errorbar(data=diffdiffall, aes(ymin=coef-1.64*stderr, ymax= coef+1.64*stderr),  colour = "black", width=0, size=3) +
  theme_bw() +theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.line = element_line(colour = "black")) +
  theme(legend.position="none") +
  theme(legend.title = element_blank()) +
  labs(x="Days Since Last School Shooting", y="Effect on Google Searches for Gun Control (Std.)", title="") +
  theme(text = element_text(size=30)) + 
  scale_y_continuous(limits=c(-1.3, 1)) +
  scale_x_continuous(breaks = scales::pretty_breaks(n = 10)) 

plot1 

ggsave(plot1, file="taper_off.png", width=10, height=6, scale=2)


